package com.zhang.discount.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.util.time.TimeUtils;
import com.zhang.discount.dto.FlashSaleSearchDTO;
import com.zhang.discount.mapper.FlashSalesMapper;
import com.zhang.discount.entity.FlashSales;
import com.zhang.discount.service.FlashSalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 创世神
 * @since 2024-09-28 09:48:51
 */
@Service
@Slf4j
public class FlashSalesServiceImpl extends ServiceImpl<FlashSalesMapper, FlashSales> implements FlashSalesService {

    @Autowired
    private FlashSalesMapper flashSalesMapper;

    @Override
    public Page<FlashSales> findPage(FlashSales params) {
        Page<FlashSales> page = new Page<>(1, 10);
        LambdaQueryWrapper<FlashSales> query = Wrappers.lambdaQuery(FlashSales.class);
        return flashSalesMapper.selectPage(page, query);
    }

    @Override
    public Page findList(FlashSaleSearchDTO dto) {
        LambdaQueryWrapper<FlashSales> wrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isBlank(dto.getSearchValue())) {
            wrapper.or().eq(FlashSales::getTitle, dto.getSearchValue())
                    .or().eq(FlashSales::getId, dto.getSearchValue());
        }
        return flashSalesMapper.selectPage(new Page<FlashSales>(dto.getPageNum(), dto.getPageSize()), wrapper);
    }

    @Override
    public FlashSales findById(Long id) {
        return flashSalesMapper.selectById(id);
    }

    @Override
    public boolean insert(FlashSales flashSales) {
        flashSales.setCreateTime(LocalDateTime.now());
        return save(flashSales);
    }

    @Override
    public boolean update(FlashSales flashSales) {
        return updateById(flashSales);
    }

    @Override
    public int delete(Long id) {
        return flashSalesMapper.deleteById(id);
    }

}
